1311B - WeirdSort - CodeForces Solution


dfs and similar sortings *1200

Please click on ads to support us..

Python Code:

t = int(input())

for _ in range(t):
    n, m = map(int, input().split())

    l = [False] * n

    arr = list(map(int, input().split()))
    pi = list(map(int, input().split()))

    for p in pi:
        l[p-1] = True

    f = True
    i = 0

    while i < n:
        if not l[i]:
            i += 1
            continue
        idx = i
        while idx < n and l[idx]:
            idx += 1
        arr[i:idx+1] = sorted(arr[i:idx+1])
        i = idx

    for i in range(len(arr)-1):
        if arr[i] > arr[i+1]:
            f = False
            break

    print("YES" if f else "NO")








C++ Code:

#include<bits/stdc++.h>
using namespace std;



#define MOD 1000000007
#define MOD1 998244353
#define INF 1e18
#define nline "\n"
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
// typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) { cerr << t; }
void _print(int t) { cerr << t; }
void _print(string t) { cerr << t; }
void _print(char t) { cerr << t; }
void _print(lld t) { cerr << t; }
void _print(double t) { cerr << t; }
void _print(ull t) { cerr << t; }

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}"; }
template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }
template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }
template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }
template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; }

void solve()
{
    int n;
    cin >> n;
    int m;
    cin >> m;
    vector<int> arr(n);
    vector<int> pos(m);
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    for (int i = 0;i < m;i++)
    {
        cin >> pos[i];
        pos[i]--;
    }
    sort(all(pos));
    vector<int> sarray = arr;
    sort(all(sarray));
    vector<pair<int, int> > positions;

    for (int ind = 0;ind < m;ind++)
    {
        int i = ind;
        int j = ind + 1;
        while (j < m && pos[j] - pos[j - 1] == 1)
        {
            j++;
        }
        j--;
        debug(pos[i]);
        debug(pos[j]);

        if (i == j)
        {
            sort(arr.begin() + pos[i], arr.begin() + pos[i] + 1 + 1);
            debug(arr);
        }
        else
        {
            sort(arr.begin() + pos[i], arr.begin() + pos[j] + 1 + 1);
        }
    }
    // sort(arr.begin(), arr.begin() + 2);
    debug(arr);
    debug(sarray);
    debug(pos);
    int flag = 0;
    for (int i = 0; i < arr.size(); i++)
    {
        if (arr[i] != sarray[i])
        {
            flag = 1;
            break;
        }
    }
    if (flag == 1)
    {
        cout << "NO\n";
        return;
    }
    else
    {
        cout << "YES\n";
    }




}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int tc = 1;
    cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case #" << t << ": ";

        solve();
    }
}


Comments

Submit
0 Comments
More Questions

1511C - Yet Another Card Deck
1698A - XOR Mixup
1702E - Split Into Two Sets
1703B - ICPC Balloons
1702F - Equate Multisets
1700A - Optimal Path
665C - Simple Strings
1708A - Difference Operations
1703E - Mirror Grid
1042A - Benches
1676B - Equal Candies
1705B - Mark the Dust Sweeper
1711A - Perfect Permutation
1701B - Permutation
1692A - Marathon
1066A - Vova and Train
169B - Replacing Digits
171D - Broken checker
380C - Sereja and Brackets
1281B - Azamon Web Services
1702A - Round Down the Price
1681C - Double Sort
12A - Super Agent
1709A - Three Doors
1680C - Binary String
1684B - Z mod X = C
1003A - Polycarp's Pockets
1691B - Shoe Shuffling
1706A - Another String Minimization Problem
1695B - Circle Game